//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Communication;
using SoapTrace.Core.Diagnostics;
using System;
using System.IO;
using System.Net;
using System.Text;

namespace SoapTrace.Tests.SoapPacketMonitoring
{
	/// <summary>
	/// Tests that the <see cref="SoapPacketMonitorBase"/> class's
	/// handling of a client that we missed the connection and is making 
	/// two soap requests, the first which we catch in the middle.
	/// </summary>
	[ TestFixture ]
	public class MidStreamTestFixture : SoapPacketMonitorBase
	{
		SoapRequest Request;
		int SoapRequestCount;
		SoapResponse Response;
		int SoapResponseCount;
		
		string ExpectedRequestMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
				"<soap:Body><GetMaxCommentListLength xmlns=\"http://codeproject.com/webservices/\" /></soap:Body>" +
				"</soap:Envelope>";
		
		string ExpectedRequestHeader = "POST /webservices/latest.asmx HTTP/1.1\r\n" +
				"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 1.1.4322.573)\r\n" +
				"Content-Type: text/xml; charset=utf-8\r\n" +
				"SOAPAction: \"http://codeproject.com/webservices/GetMaxCommentListLength\"\r\n" +
				"Content-Length: 317\r\n" +
				"Expect: 100-continue\r\n" +
				"Connection: Keep-Alive\r\n" +
				"Host: www.codeproject.com\r\n" +
				"\r\n";
		
		string ExpectedResponseMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
				"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
				"<soap:Body><GetMaxCommentListLengthResponse xmlns=\"http://codeproject.com/webservices/\">" + 
				"<GetMaxCommentListLengthResult>10</GetMaxCommentListLengthResult>" +
				"</GetMaxCommentListLengthResponse>" +
				"</soap:Body>" +
				"</soap:Envelope>";

		string ExpectedResponseHeader = "HTTP/1.1 200 OK\r\n" +
				"Server: Microsoft-IIS/5.0\r\n" +
				"Date: Mon, 31 Jan 2005 18:43:11 GMT\r\n" +
				"X-Powered-By: ASP.NET\r\n" +
				"X-AspNet-Version: 1.1.4322\r\n" +
				"Cache-Control: no-cache\r\n" +
				"Pragma: no-cache\r\n" +
				"Expires: -1\r\n" +
				"Content-Type: text/xml; charset=utf-8\r\n" +
				"Content-Length: 422\r\n" +
				"\r\n";
		
		/// <summary>
		/// Initialises the test fixture.
		/// </summary>
		[ TestFixtureSetUp ]
		public void Init( )
		{
			Request = null;
			Response = null;
			
			base.ClearClients( );
			base.SetFilter( "209.171.52.99", 80 );
			
			// Send first partial request.
			
			IPEndPoint ClientEndPoint = new IPEndPoint( IPAddress.Parse( "192.168.1.3" ), 7002 );
			IPEndPoint ServerEndPoint = new IPEndPoint( IPAddress.Parse( "209.171.52.99" ), 80 );
			                                           
			byte[ ] Bytes = GetBytes( "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
				"<soap:Body><GetLoungeListLength xmlns=\"http://codeproject.com/webservices/\" /></soap:Body>" +
				"</soap:Envelope>" );
			
			TcpCommunicationEventArgs EventArgs = 
				new TcpCommunicationEventArgs( ClientEndPoint, ServerEndPoint, Bytes );	
			base.MissedConnectionBytesTransferred( this, EventArgs );
			
			// Send first response.
			
			Bytes = GetBytes( "HTTP/1.1 200 OK\r\n" +
				"Content-Type: text/xml; charset=utf-8\r\n" +
				"Content-Length: 422\r\n" +
				"\r\n" +
				"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
				"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
				"<soap:Body><GetMaxCommentListLengthResponse xmlns=\"http://codeproject.com/webservices/\">" + 
				"<GetMaxCommentListLengthResult>10</GetMaxCommentListLengthResult>" +
				"</GetMaxCommentListLengthResponse>" +
				"</soap:Body>" +
				"</soap:Envelope>" );
			
			EventArgs = new TcpCommunicationEventArgs( ServerEndPoint, ClientEndPoint, Bytes );			
			base.MissedConnectionBytesTransferred( this, EventArgs );
			
			// Send second request.
			
			Bytes = GetBytes( String.Concat( ExpectedRequestHeader, ExpectedRequestMessage ) );
			EventArgs = new TcpCommunicationEventArgs( ClientEndPoint, ServerEndPoint, Bytes );	
			base.MissedConnectionBytesTransferred( this, EventArgs );
			
			// Send second response.
			
			Bytes = GetBytes( String.Concat( ExpectedResponseHeader, ExpectedResponseMessage ) );
			EventArgs = new TcpCommunicationEventArgs( ServerEndPoint, ClientEndPoint, Bytes );
			base.MissedConnectionBytesTransferred( this, EventArgs );
		}
			
		/// <summary>
		/// Check we received the correct message.
		/// </summary>
		[ Test ]
		public void SoapRequestMessage( )
		{			
			Assert.AreEqual( ExpectedRequestMessage, Request.Message, "Unexpected message received." );
		}
		
		/// <summary>
		/// Check the soap request header.
		/// </summary>
		[ Test ]
		public void SoapRequestHeader( )
		{
			Assert.AreEqual( ExpectedRequestHeader, Request.Header, "Unexpected request header received." );
		}
		
		/// <summary>
		/// Check we received the correct message.
		/// </summary>
		[ Test ]
		public void SoapResponseMessage( )
		{			
			Assert.AreEqual( ExpectedResponseMessage, Response.Message, "Unexpected message received." );
		}
		
		/// <summary>
		/// Check that the soap response header is correct.
		/// </summary>
		[ Test ]
		public void SoapResponseHeader( )
		{
			Assert.AreEqual( ExpectedResponseHeader, Response.Header, "Unexpected response header." );
		}
	
		/// <summary>
		/// Handles the soap request received event.
		/// </summary>
		protected override void OnSoapRequestReceived( object sender, SoapRequestReceivedEventArgs e )
		{
			Request = e.Request;
			SoapRequestCount++;
		}
		
		/// <summary>
		/// Handles the soap response received event.
		/// </summary>
		protected override void OnSoapResponseReceived( object sender, SoapResponseReceivedEventArgs e )
		{
			Response = e.Response;
			SoapResponseCount++;
		}		
		
		byte[ ] GetBytes( string text )
		{
			return UnicodeEncoding.UTF8.GetBytes( text );
		}
	}
}
